package zd.cornermemory.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import zd.cornermemory.R;
import zd.cornermemory.fragment.remember.CEFragment;
import zd.cornermemory.fragment.remember.CornerFragment;
import zd.cornermemory.fragment.remember.EdgeFragment;
import zd.cornermemory.ui.CodeActivity;
import zd.cornermemory.ui.RemeberCjActivity;
import zd.cornermemory.utils.SPUtils;
import zd.cornermemory.utils.SpKey;

/**
 * Created by zhud on 2019/4/29.
 */

public class RememberFragment extends Fragment implements View.OnClickListener{
    //三个Tab分别对应的Fragment
    private CornerFragment mFrag1;
    private EdgeFragment mFrag2;
    private CEFragment mFrag3;
    private String currentType = "0";
    private TextView  cjlb_button;
    private ImageView cjlb_img;
    private TextView  tab1,tab2,tab3;
    private TextView code;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab1, container, false);
        cjlb_button = view.findViewById(R.id.cjlb_button);
        cjlb_img = view.findViewById(R.id.cjlb_img);
        tab1 = view.findViewById(R.id.tab1);
        tab2 = view.findViewById(R.id.tab2);
        tab3 = view.findViewById(R.id.tab3);
        code = view.findViewById(R.id.code);
        cjlb_button.setOnClickListener(this);
        tab1.setOnClickListener(this);
        tab2.setOnClickListener(this);
        tab3.setOnClickListener(this);
        code.setOnClickListener(this);
        cjlb_img.setOnClickListener(this);
        currentType = SPUtils.getInstance().getString(SpKey.REMEMBER_LX_TYPE,"0");
        int tab = Integer.parseInt(currentType);
        selectTab(tab);
        return view;
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cjlb_button:
                gotoCj();
                break;
            case R.id.cjlb_img:
                gotoCj();
                break;
            case R.id.tab1:
                selectTab(0);
                break;
            case R.id.tab2:
                selectTab(1);
                break;
            case R.id.tab3:
                selectTab(2);
                break;
            case R.id.code:
                Intent intent = new Intent(getActivity(), CodeActivity.class);
                startActivity(intent);
                break;
        }
    }

    private void gotoCj() {
        currentType = SPUtils.getInstance().getString(SpKey.REMEMBER_LX_TYPE,"0");
        int tab = Integer.parseInt(currentType);
        Intent intent = new Intent(getActivity(), RemeberCjActivity.class);
        Bundle bundle = new Bundle();
        int team = 0;
        if (tab==0) {
            team = mFrag1.getTeam();
        }else if(tab ==1){
            team = mFrag2.getTeam();
        }
        bundle.putInt("currentPos",team);
        bundle.putInt("type",tab);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    //进行选中Tab的处理
    private void selectTab(int i) {
        resetImgs();
        //获取FragmentManager对象
        FragmentManager manager = getChildFragmentManager();
        //获取FragmentTransaction对象
        FragmentTransaction transaction = manager.beginTransaction();
        //先隐藏所有的Fragment
        hideFragments(transaction);
        switch (i) {
            //当选中点击Tab时
            case 0:
                SPUtils.getInstance().put(SpKey.REMEMBER_LX_TYPE,"0");
                tab1.setBackgroundResource(R.drawable.jy_tab_pressed_bg);
                //如果对应的Fragment没有实例化，则进行实例化，并显示出来
                if (mFrag1 == null) {
                    mFrag1 = new CornerFragment();
                    transaction.add(R.id.id_content, mFrag1);
                } else {
                    //如果对应的Fragment已经实例化，则直接显示出来
                    transaction.show(mFrag1);
                }
                break;
            case 1:
                SPUtils.getInstance().put(SpKey.REMEMBER_LX_TYPE,"1");
                tab2.setBackgroundResource(R.drawable.jy_tab_pressed_bg);
                if (mFrag2 == null) {
                    mFrag2 = new EdgeFragment();
                    transaction.add(R.id.id_content, mFrag2);
                } else {
                    transaction.show(mFrag2);
                }
                break;
            case 2:
                SPUtils.getInstance().put(SpKey.REMEMBER_LX_TYPE,"2");
                tab3.setBackgroundResource(R.drawable.jy_tab_pressed_bg);
                if (mFrag3 == null) {
                    mFrag3 = new CEFragment();
                    transaction.add(R.id.id_content, mFrag3);
                } else {
                    transaction.show(mFrag3);
                }
                break;

        }
        //提交事务
        transaction.commit();
    }

    private void hideFragments(FragmentTransaction transaction) {
        if (mFrag1 != null) {
            transaction.hide(mFrag1);
        }
        if (mFrag2 != null) {
            transaction.hide(mFrag2);
        }
        if (mFrag3 != null) {
            transaction.hide(mFrag3);
        }
    }

    private void resetImgs() {
        tab1.setBackgroundResource(R.color.jy_tab_color);
        tab2.setBackgroundResource(R.color.jy_tab_color);
        tab3.setBackgroundResource(R.color.jy_tab_color);
    }


}
